
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>路由设计 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3.controller.html" />
    
    
    <link rel="prev" href="1.plan.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../开源组件实现.html">
            
                <a href="../开源组件实现.html">
            
                    
                    开源组件实现
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="MVC框架实现篇.html">
            
                <a href="MVC框架实现篇.html">
            
                    
                    MVC框架实现篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1.1" data-path="./">
            
                <a href="./">
            
                    
                    MVC框架实现篇
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.2" data-path="1.plan.html">
            
                <a href="1.plan.html">
            
                    
                    项目规划
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.1.3" data-path="2.route.html">
            
                <a href="2.route.html">
            
                    
                    路由设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.4" data-path="3.controller.html">
            
                <a href="3.controller.html">
            
                    
                    控制器设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.5" data-path="4.config.html">
            
                <a href="4.config.html">
            
                    
                    配置设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.6" data-path="5.view.html">
            
                <a href="5.view.html">
            
                    
                    视图设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.7" data-path="6.dbutil.html">
            
                <a href="6.dbutil.html">
            
                    
                    数据库操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.1.8" data-path="7.crud.html">
            
                <a href="7.crud.html">
            
                    
                    增删改查
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../ioc/IOC容器实现篇.html">
            
                <a href="../ioc/IOC容器实现篇.html">
            
                    
                    IOC容器实现篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.2.1" data-path="../ioc/1.concept.html">
            
                <a href="../ioc/1.concept.html">
            
                    
                    IOC的概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.2" data-path="../ioc/2.spring.html">
            
                <a href="../ioc/2.spring.html">
            
                    
                    Spring中怎么用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.3" data-path="../ioc/3.myioc.html">
            
                <a href="../ioc/3.myioc.html">
            
                    
                    设计一个IOC
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2.4" data-path="../ioc/4.principle.html">
            
                <a href="../ioc/4.principle.html">
            
                    
                    原理分析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../设计模式系列.html">
            
                <a href="../设计模式系列.html">
            
                    
                    设计模式系列
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../designpatterns/singleton.html">
            
                <a href="../designpatterns/singleton.html">
            
                    
                    如何正确地写出单例模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../designpatterns/proxy.html">
            
                <a href="../designpatterns/proxy.html">
            
                    
                    代理模式剖析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../designpatterns/strategy.html">
            
                <a href="../designpatterns/strategy.html">
            
                    
                    什么是策略模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../Java8系列.html">
            
                <a href="../Java8系列.html">
            
                    
                    Java8系列
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../java8/java8-guide.html">
            
                <a href="../java8/java8-guide.html">
            
                    
                    Java8简明教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../java8/foreach.html">
            
                <a href="../java8/foreach.html">
            
                    
                    Java8 Foreach
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../Hexo搭建博客.html">
            
                <a href="../Hexo搭建博客.html">
            
                    
                    Hexo搭建博客
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../hexo/hello.html">
            
                <a href="../hexo/hello.html">
            
                    
                    分分钟部署一个Hexo环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../hexo/config.html">
            
                <a href="../hexo/config.html">
            
                    
                    各种配置详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../hexo/writing.html">
            
                <a href="../hexo/writing.html">
            
                    
                    开始写作吧
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../开发者指南.html">
            
                <a href="../开发者指南.html">
            
                    
                    开发者指南
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../git/guide.html">
            
                <a href="../git/guide.html">
            
                    
                    git - 简明指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" >
            
                <a target="_blank" href="https://waylau.gitbooks.io/jersey-2-user-guide/content/index.html">
            
                    
                    Jersey-2.x用户指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" >
            
                <a target="_blank" href="https://waylau.gitbooks.io/rest-in-action/content/">
            
                    
                    REST 实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" >
            
                <a target="_blank" href="https://github.com/waylau/servlet-3.1-specification">
            
                    
                    Java Servlet 3.1 规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" >
            
                <a target="_blank" href="http://mybatis.github.io/mybatis-3/zh/index.html">
            
                    
                    MyBatis中文指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" >
            
                <a target="_blank" href="https://github.com/waylau/apache-shiro-1.2.x-reference">
            
                    
                    Apache Shiro 用户指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" >
            
                <a target="_blank" href="https://github.com/qibaoguang/Spring-Boot-Reference-Guide/blob/master/SUMMARY.md">
            
                    
                    Spring Boot参考指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" >
            
                <a target="_blank" href="https://github.com/waylau/netty-4-user-guide/blob/master/SUMMARY.md">
            
                    
                    Netty4 用户指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../user_guide/google-java8-guide.html">
            
                <a href="../user_guide/google-java8-guide.html">
            
                    
                    Google Java编程风格指南
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../运维相关.html">
            
                <a href="../运维相关.html">
            
                    
                    运维相关
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../web/test_tool.html">
            
                <a href="../web/test_tool.html">
            
                    
                    Web性能测试工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" >
            
                <a target="_blank" href="https://github.com/judasn/Linux-Tutorial">
            
                    
                    Java 程序员眼中的 Linux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../learn_server/">
            
                <a href="../learn_server/">
            
                    
                    写给java开发的运维笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../经典文章.html">
            
                <a href="../经典文章.html">
            
                    
                    经典文章
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../articles/request_message.html">
            
                <a href="../articles/request_message.html">
            
                    
                    HTTP请求报文解剖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../articles/software_architecture_patterns.html">
            
                <a href="../articles/software_architecture_patterns.html">
            
                    
                    软件架构模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../articles/electrical-business-architecture.html">
            
                <a href="../articles/electrical-business-architecture.html">
            
                    
                    电商网站架构案例
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../服务器相关.html">
            
                <a href="../服务器相关.html">
            
                    
                    服务器/域名/SSL证书
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" >
            
                <a target="_blank" href="https://domainr.com">
            
                    
                    可用域名
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" >
            
                <a target="_blank" href="http://hk.hostsir.com/Affiliates/i/d/976">
            
                    
                    优惠域名
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" >
            
                <a target="_blank" href="http://www.vultr.com/?ref=6886447">
            
                    
                    vultr VPS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" >
            
                <a target="_blank" href="https://sshdropbear.net/">
            
                    
                    免费VPN/SSH服务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" >
            
                <a target="_blank" href="http://get.ishadow.website/">
            
                    
                    免费SS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" >
            
                <span>
            
                    
                    1小时免费VPN
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.7" >
            
                <a target="_blank" href="https://saki.ssl.do/aff.php?aff=11">
            
                    
                    SSL.DO
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.8" >
            
                <a target="_blank" href="http://api.hostinger.com.hk/redir/22018310">
            
                    
                    免费虚拟主机
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../实用工具.html">
            
                <a href="../实用工具.html">
            
                    
                    实用工具/API
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" >
            
                <a target="_blank" href="https://tinypng.com/">
            
                    
                    PNG图片无损压缩
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" >
            
                <a target="_blank" href="http://tool.c7sky.com/image-watermark/">
            
                    
                    在线给图片加水印
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" >
            
                <a target="_blank" href="http://tool.c7sky.com/password/">
            
                    
                    随机密码生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" >
            
                <a target="_blank" href="https://randomuser.me/">
            
                    
                    随机头像生成
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" >
            
                <a target="_blank" href="http://tool.c7sky.com/tcleaner/">
            
                    
                    微博一键清理工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" >
            
                <a target="_blank" href="http://csscompressor.com/">
            
                    
                    CSS压缩
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" >
            
                <a target="_blank" href="http://www.atool.org/">
            
                    
                    在线工具
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >路由设计</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x8DEF;&#x7531;&#x8BBE;&#x8BA1;">&#x8DEF;&#x7531;&#x8BBE;&#x8BA1;</h1>
<p>&#x73B0;&#x4EE3; Web &#x5E94;&#x7528;&#x7684; URL &#x5341;&#x5206;&#x4F18;&#x96C5;&#xFF0C;&#x6613;&#x4E8E;&#x4EBA;&#x4EEC;&#x8FA8;&#x8BC6;&#x8BB0;&#x5FC6;&#x3002; &#x8DEF;&#x7531;&#x7684;&#x8868;&#x73B0;&#x5F62;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>/resources/:resource/actions/:action
http://bladejava.com
http://bladejava.com/docs/modules/route
</code></pre><p>&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x5728;java&#x8BED;&#x8A00;&#x4E2D;&#x5C06;&#x4ED6;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A; <code>Route</code> &#x7C7B;, &#x7528;&#x4E8E;&#x5C01;&#x88C5;&#x4E00;&#x4E2A;&#x8BF7;&#x6C42;&#x7684;&#x6700;&#x5C0F;&#x5355;&#x5143;&#xFF0C;
&#x5728;Mario&#x4E2D;&#x6211;&#x4EEC;&#x8BBE;&#x8BA1;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x7684;&#x5BF9;&#x8C61;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * &#x8DEF;&#x7531;
 * <span class="hljs-doctag">@author</span> biezhi
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Route</span> </span>{

    <span class="hljs-comment">/**
     * &#x8DEF;&#x7531;path
     */</span>
    <span class="hljs-keyword">private</span> String path;

    <span class="hljs-comment">/**
     * &#x6267;&#x884C;&#x8DEF;&#x7531;&#x7684;&#x65B9;&#x6CD5;
     */</span>
    <span class="hljs-keyword">private</span> Method action;

    <span class="hljs-comment">/**
     * &#x8DEF;&#x7531;&#x6240;&#x5728;&#x7684;&#x63A7;&#x5236;&#x5668;
     */</span>
    <span class="hljs-keyword">private</span> Object controller;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Route</span><span class="hljs-params">()</span> </span>{
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getPath</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> path;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPath</span><span class="hljs-params">(String path)</span> </span>{
        <span class="hljs-keyword">this</span>.path = path;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> Method <span class="hljs-title">getAction</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> action;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAction</span><span class="hljs-params">(Method action)</span> </span>{
        <span class="hljs-keyword">this</span>.action = action;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> Object <span class="hljs-title">getController</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> controller;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setController</span><span class="hljs-params">(Object controller)</span> </span>{
        <span class="hljs-keyword">this</span>.controller = controller;
    }

}
</code></pre>
<p>&#x6240;&#x6709;&#x7684;&#x8BF7;&#x6C42;&#x5728;&#x7A0B;&#x5E8F;&#x4E2D;&#x662F;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#xFF0C;&#x5339;&#x914D;&#x5728; <code>path</code> &#x4E0A;&#xFF0C;&#x6267;&#x884C;&#x9760; <code>action</code>&#xFF0C;&#x5904;&#x4E8E; <code>controller</code> &#x4E2D;&#x3002;</p>
<p>Mario&#x4F7F;&#x7528;&#x4E00;&#x4E2A;Filter&#x63A5;&#x6536;&#x6240;&#x6709;&#x8BF7;&#x6C42;&#xFF0C;&#x56E0;&#x4E3A;&#x4ECE;Filter&#x8FC7;&#x6765;&#x7684;&#x8BF7;&#x6C42;&#x6709;&#x65E0;&#x6570;&#xFF0C;&#x5982;&#x4F55;&#x77E5;&#x9053;&#x54EA;&#x4E00;&#x4E2A;&#x8BF7;&#x6C42;&#x5BF9;&#x5E94;&#x54EA;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x5462;&#xFF1F;
&#x8FD9;&#x65F6;&#x5019;&#x9700;&#x8981;&#x8BBE;&#x8BA1;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x5339;&#x914D;&#x5668;&#x53BB;&#x67E5;&#x627E;&#x8DEF;&#x7531;&#x5904;&#x7406;&#x6211;&#x4EEC;&#x914D;&#x7F6E;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;
&#x6709;&#x4E86;&#x8DEF;&#x7531;&#x5339;&#x914D;&#x5668;&#x8FD8;&#x4E0D;&#x591F;&#xFF0C;&#x8FD9;&#x4E48;&#x591A;&#x7684;&#x8DEF;&#x7531;&#x6211;&#x4EEC;&#x5982;&#x4F55;&#x7BA1;&#x7406;&#x5462;&#xFF1F;&#x518D;&#x6765;&#x4E00;&#x4E2A;&#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x5668;&#x5427;&#xFF0C;&#x4E0B;&#x9762;&#x5C31;&#x521B;&#x5EFA;&#x8DEF;&#x7531;&#x5339;&#x914D;&#x5668;&#x548C;&#x7BA1;&#x7406;&#x5668;2&#x4E2A;&#x7C7B;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * &#x8DEF;&#x7531;&#x7BA1;&#x7406;&#x5668;&#xFF0C;&#x5B58;&#x653E;&#x6240;&#x6709;&#x8DEF;&#x7531;&#x7684;
 * <span class="hljs-doctag">@author</span> biezhi
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Routers</span> </span>{

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Logger LOGGER = Logger.getLogger(Routers.class.getName());

    <span class="hljs-keyword">private</span> List&lt;Route&gt; routes = <span class="hljs-keyword">new</span> ArrayList&lt;Route&gt;();

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Routers</span><span class="hljs-params">()</span> </span>{
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addRoute</span><span class="hljs-params">(List&lt;Route&gt; routes)</span></span>{
        routes.addAll(routes);
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addRoute</span><span class="hljs-params">(Route route)</span></span>{
        routes.add(route);
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeRoute</span><span class="hljs-params">(Route route)</span></span>{
        routes.remove(route);
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">addRoute</span><span class="hljs-params">(String path, Method action, Object controller)</span></span>{
        Route route = <span class="hljs-keyword">new</span> Route();
        route.setPath(path);
        route.setAction(action);
        route.setController(controller);

        routes.add(route);
        LOGGER.info(<span class="hljs-string">&quot;Add Route&#xFF1A;[&quot;</span> + path + <span class="hljs-string">&quot;]&quot;</span>);
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> List&lt;Route&gt; <span class="hljs-title">getRoutes</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> routes;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setRoutes</span><span class="hljs-params">(List&lt;Route&gt; routes)</span> </span>{
        <span class="hljs-keyword">this</span>.routes = routes;
    }

}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x7684;&#x4EE3;&#x7801;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x8FD9;&#x4E2A;&#x7BA1;&#x7406;&#x5668;&#x91CC;&#x7528;List&#x5B58;&#x50A8;&#x6240;&#x6709;&#x8DEF;&#x7531;&#xFF0C;&#x516C;&#x6709;&#x7684; <code>addRoute</code> &#x65B9;&#x6CD5;&#x662F;&#x7ED9;&#x5916;&#x90E8;&#x8C03;&#x7528;&#x7684;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * &#x8DEF;&#x7531;&#x5339;&#x914D;&#x5668;&#xFF0C;&#x7528;&#x4E8E;&#x5339;&#x914D;&#x8DEF;&#x7531;
 * <span class="hljs-doctag">@author</span> biezhi
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RouteMatcher</span> </span>{

    <span class="hljs-keyword">private</span> List&lt;Route&gt; routes;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">RouteMatcher</span><span class="hljs-params">(List&lt;Route&gt; routes)</span> </span>{
        <span class="hljs-keyword">this</span>.routes = routes;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setRoutes</span><span class="hljs-params">(List&lt;Route&gt; routes)</span> </span>{
        <span class="hljs-keyword">this</span>.routes = routes;
    }

    <span class="hljs-comment">/**
     * &#x6839;&#x636E;path&#x67E5;&#x627E;&#x8DEF;&#x7531;
     * <span class="hljs-doctag">@param</span> path    &#x8BF7;&#x6C42;&#x5730;&#x5740;
     * <span class="hljs-doctag">@return</span>        &#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x5230;&#x7684;&#x8DEF;&#x7531;
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Route <span class="hljs-title">findRoute</span><span class="hljs-params">(String path)</span> </span>{
        String cleanPath = parsePath(path);
        List&lt;Route&gt; matchRoutes = <span class="hljs-keyword">new</span> ArrayList&lt;Route&gt;();
        <span class="hljs-keyword">for</span> (Route route : <span class="hljs-keyword">this</span>.routes) {
            <span class="hljs-keyword">if</span> (matchesPath(route.getPath(), cleanPath)) {
                matchRoutes.add(route);
            }
        }
        <span class="hljs-comment">// &#x4F18;&#x5148;&#x5339;&#x914D;&#x539F;&#x5219;</span>
        giveMatch(path, matchRoutes);

        <span class="hljs-keyword">return</span> matchRoutes.size() &gt; <span class="hljs-number">0</span> ? matchRoutes.get(<span class="hljs-number">0</span>) : <span class="hljs-keyword">null</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">giveMatch</span><span class="hljs-params">(<span class="hljs-keyword">final</span> String uri, List&lt;Route&gt; routes)</span> </span>{
        Collections.sort(routes, <span class="hljs-keyword">new</span> Comparator&lt;Route&gt;() {
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">compare</span><span class="hljs-params">(Route o1, Route o2)</span> </span>{
                <span class="hljs-keyword">if</span> (o2.getPath().equals(uri)) {
                    <span class="hljs-keyword">return</span> o2.getPath().indexOf(uri);
                }
                <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
            }
        });
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">matchesPath</span><span class="hljs-params">(String routePath, String pathToMatch)</span> </span>{
        routePath = routePath.replaceAll(PathUtil.VAR_REGEXP, PathUtil.VAR_REPLACE);
        <span class="hljs-keyword">return</span> pathToMatch.matches(<span class="hljs-string">&quot;(?i)&quot;</span> + routePath);
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> String <span class="hljs-title">parsePath</span><span class="hljs-params">(String path)</span> </span>{
        path = PathUtil.fixPath(path);
        <span class="hljs-keyword">try</span> {
            URI uri = <span class="hljs-keyword">new</span> URI(path);
            <span class="hljs-keyword">return</span> uri.getPath();
        } <span class="hljs-keyword">catch</span> (URISyntaxException e) {
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>;
        }
    }

}
</code></pre>
<p>&#x8DEF;&#x7531;&#x5339;&#x914D;&#x5668;&#x4F7F;&#x7528;&#x4E86;&#x6B63;&#x5219;&#x53BB;&#x904D;&#x5386;&#x8DEF;&#x7531;&#x5217;&#x8868;&#xFF0C;&#x5339;&#x914D;&#x5408;&#x9002;&#x7684;&#x8DEF;&#x7531;&#x3002;&#x5F53;&#x7136;&#x6211;&#x4E0D;&#x8BA4;&#x4E3A;&#x8FD9;&#x662F;&#x6700;&#x597D;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;
&#x56E0;&#x4E3A;&#x8DEF;&#x7531;&#x7684;&#x91CF;&#x5F88;&#x5927;&#x4E4B;&#x540E;&#x904D;&#x5386;&#x7684;&#x6548;&#x7387;&#x4F1A;&#x964D;&#x4F4E;&#xFF0C;&#x4F46;&#x8FD9;&#x6837;&#x662F;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x6709;&#x66F4;&#x597D;&#x7684;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x544A;&#x8BC9;&#x6211; :)</p>
<p>&#x5728;&#x4E0B;&#x4E00;&#x7AE0;&#x8282;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5BF9;&#x8BF7;&#x6C42;&#x5904;&#x7406;&#x505A;&#x8BBE;&#x8BA1;&#x4E86;~</p>
<h2 id="links">links</h2>
<ul>
<li><a href="./">&#x76EE;&#x5F55;</a></li>
<li>&#x4E0A;&#x4E00;&#x8282;: <a href="1.plan.html">&#x9879;&#x76EE;&#x89C4;&#x5212;</a></li>
<li>&#x4E0B;&#x4E00;&#x8282;: <a href="3.controller.html">&#x63A7;&#x5236;&#x5668;&#x8BBE;&#x8BA1;</a></li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="1.plan.html" class="navigation navigation-prev " aria-label="Previous page: 项目规划">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3.controller.html" class="navigation navigation-next " aria-label="Next page: 控制器设计">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"路由设计","level":"1.2.1.3","depth":3,"next":{"title":"控制器设计","level":"1.2.1.4","depth":3,"path":"mvc/3.controller.md","ref":"mvc/3.controller.md","articles":[]},"previous":{"title":"项目规划","level":"1.2.1.2","depth":3,"path":"mvc/1.plan.md","ref":"mvc/1.plan.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"mvc/2.route.md","mtime":"2017-09-01T15:31:07.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-09-01T16:57:38.335Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

